//
//  ALNotification.swift
//  ALCore
//
//  Created by IdeaBank on 2023/12/18.
//

import Foundation

/*

 简化本地通知的使用，建议按照样例方式进行使用
 使用 enum 来管理所有的本地通知，可以保证不重复

 Example:

 extension NotificationCenter {

    enum UserCenter: String, NotificationPostAndRegister {

        case didLogin = "didLogin"

        var name: NSNotification.Name {
            get {
                return NSNotification.Name(rawValue: rawValue)
            }
        }
    }
 }

 Use:

 NotificationCenter.UserCenter.didLogin.post(nil)

 NotificationCenter.UserCenter.didLogin.register(self, selector: @selector(didLogin))

 NotificationCenter.UserCenter.didLogin.remove(self)

 */
///通知
public protocol ALNotificationPostAndRegister {

    var name: NSNotification.Name { get }

    func register(observer: Any, selector: Selector)

    func post(object: Any?)

    func remove(observer: Any)
}

public extension ALNotificationPostAndRegister {

    func register(observer: Any, selector: Selector) {
        NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: nil)
    }

    func post(object: Any?) {
        NotificationCenter.default.post(name: name, object: object)
    }

    func remove(observer: Any) {
        NotificationCenter.default.removeObserver(observer, name: name, object: nil)
    }

}
